<?php

namespace App\Sdks\Feishu;

use App\Common\Tools\CustomException;
use App\Sdks\Feishu\Traits\App;
use App\Sdks\Feishu\Traits\Contact;
use App\Sdks\Feishu\Traits\Employee;
use App\Sdks\Feishu\Traits\Post;
use App\Sdks\Feishu\Traits\Request;
use App\Sdks\Feishu\Traits\Text;
use App\Sdks\Feishu\Traits\Token;

class Feishu
{
    use App;
    use Contact;
    use Employee;
    use Request;
    use Post;
    use Text;
    use Token;

    /**
     * 公共接口地址
     */
    const BASE_URL = 'https://open.feishu.cn/open-apis';

    /**
     * Feishu constructor.
     * @param $appId
     * @param $appSecret
     */
    public function __construct($appId, $appSecret){
        $this->setApp($appId, $appSecret);
    }

    /**
     * @param $api
     * @return string
     * @throws CustomException
     * 获取接口地址
     */
    public function getApiUrl($api){
        $apiUriMap = [
            'get_tenant_access_token' => 'auth/v3/tenant_access_token/internal/',
            'get_scope_contact' => 'contact/v1/scope/get',
            'get_department_user_list' => 'contact/v1/department/user/list',
            'user_batch_get' => 'contact/v1/user/batch_get',
            'message_send' => 'message/v4/send/',
        ];

        if(!isset($apiUriMap[$api])){
            throw new CustomException([
                'code' => 'NOT_FOUND_API_URL',
                'message' => '找不到接口地址',
            ]);
        }

        $apiUrl = self::BASE_URL .'/'. $apiUriMap[$api];
        return $apiUrl;
    }
}
